package com.panshi.controller;

import com.panshi.api.api.AnswerCommentsHDPort;
import com.panshi.api.bean.AnswerCommentsHDBean;
import com.panshi.api.bean.AnswerCommentsLinkBean;
import com.panshi.api.util.PageSizeVO;
import com.panshi.pojo.AnswerCommentsHDVO;
import com.panshi.pojo.AnswerHDVO;
import com.panshi.pojo.TimeVO;
import com.panshi.servers.AnswerCommentsHDServers;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * @author XiaoWangYa
 * @version 1.0
 * @date 2020/8/12 0012 上午 10:09
 */
@Slf4j
@Controller
@Api("回答评论Controller层")
public class AnswerCommentsHDController {

    @Autowired
    private AnswerCommentsHDServers answerCommentsHDServers;

    @PostMapping("/addAnswerCommentsHDInfo")
    @ResponseBody
    @ApiModelProperty("回答评论新增")
    public AnswerCommentsHDBean addAnswerCommentsHDInfo(@RequestBody AnswerCommentsHDBean answerCommentsHDBean) {
        // 类型转换
//        AnswerCommentsHDVO commentsHDVO = new AnswerCommentsHDVO();
//        BeanUtils.copyProperties(answerCommentsHDBean,commentsHDVO);
        // 获取主键
        int id = answerCommentsHDServers.addAnswerCommentsHDInfo(answerCommentsHDBean);
        // 返回新增数据
        AnswerCommentsHDVO answerCommentsHDVO = answerCommentsHDServers.queryAssginComments(answerCommentsHDBean.getAcid());
        if (answerCommentsHDBean.getParentid() == 0) {  // 回答评论
            // 查询指定回答数据
            AnswerHDVO answerHDVO = answerCommentsHDServers.queryAssignAnswer(answerCommentsHDBean.getAnswer_id());
            // 增加评论量
            answerHDVO.setCount(answerHDVO.getCount() + 1);
            answerCommentsHDServers.updateAnswerCountSum(answerHDVO);

            // 数据转换
            AnswerCommentsHDBean comments = new AnswerCommentsHDBean();
            BeanUtils.copyProperties(answerCommentsHDVO, comments);
            return comments;
        } else {    // 评论回复
            PageSizeVO page = new PageSizeVO();
            page.setParentid(answerCommentsHDVO.getAcid());
            page.setDepth(Integer.valueOf(answerCommentsHDVO.getDepth()));
            // 查询回复信息
            List<AnswerCommentsHDVO> answerCommentsHDVOS = answerCommentsHDServers.queryAssginAnswerReplyInfo(page);
            // 数据转换
            AnswerCommentsHDBean comments = new AnswerCommentsHDBean();
            BeanUtils.copyProperties(answerCommentsHDVOS.get(0),comments);
            // 将时间进行转换为xxx前发布
            comments.setDataTime(TimeVO.getTimeFormatText(answerCommentsHDVO.getTime()));
            return comments;
        }
    }

    @PostMapping("/deleteUserAnswerComments/{id}")
    @ResponseBody
    @ApiModelProperty("回答评论删除")
    public void deleteUserAnswerComments(@PathVariable int id) {
        answerCommentsHDServers.deleteUserAnswerComments(id);
    }

    @PostMapping("/queryAssignAnswerCommentsAll")
    @ResponseBody
    @ApiModelProperty("查询指定回答下所有评论")
    public List<AnswerCommentsHDBean>  queryAssignAnswerCommentsAll(@RequestBody PageSizeVO pageSizeVO) {
        log.info(" 查询指定回答下所有评论 --> " + pageSizeVO);
        // 获取分业起始页
        pageSizeVO.setStartIndex(pageSizeVO.getStartIndexInfo());
        // 获取评论数据
        List<AnswerCommentsHDVO> answerCommentsHDVOS = answerCommentsHDServers.queryAssignAnswerCommentsAll(pageSizeVO);
        // 返回数据
        List<AnswerCommentsHDBean> answerComments = new ArrayList<>();
        for (AnswerCommentsHDVO answerCommentsHDVO : answerCommentsHDVOS) {
            // 数据转换
            AnswerCommentsHDBean answerCommentsHDBean = new AnswerCommentsHDBean();
            BeanUtils.copyProperties(answerCommentsHDVO,answerCommentsHDBean);
            answerCommentsHDBean.setDataTime(TimeVO.getTimeFormatText(answerCommentsHDVO.getTime()));
            answerComments.add(answerCommentsHDBean);
        }
        return answerComments;
    }

    @PostMapping("/queryAssginAnswerReplyInfo")
    @ResponseBody
    @ApiModelProperty("查询评论回复")
    public List<AnswerCommentsHDBean>  queryAssginAnswerReplyInfo(@RequestBody PageSizeVO pageSizeVO) {
        log.info(" 查询评论回复 --> " + pageSizeVO);
        // 获取分业起始页
        pageSizeVO.setStartIndex(pageSizeVO.getStartIndexInfo());
        // 获取评论数据
        List<AnswerCommentsHDVO> answerCommentsHDVOS = answerCommentsHDServers.queryAssginAnswerReplyInfo(pageSizeVO);
        // 返回数据
        List<AnswerCommentsHDBean> answerComments = new ArrayList<>();
        for (AnswerCommentsHDVO answerCommentsHDVO : answerCommentsHDVOS) {
            // 数据转换
            AnswerCommentsHDBean answerCommentsHDBean = new AnswerCommentsHDBean();
            BeanUtils.copyProperties(answerCommentsHDVO,answerCommentsHDBean);
            answerCommentsHDBean.setDataTime(TimeVO.getTimeFormatText(answerCommentsHDVO.getTime()));
            answerComments.add(answerCommentsHDBean);
        }
        return answerComments;
    }

    @PostMapping("/updateAssignAnswerCommentsLink")
    @ResponseBody
    @ApiModelProperty("点赞评论")
    public String updateAssignAnswerCommentsLink(@RequestBody AnswerCommentsLinkBean answerCommentsLinkBean) {
        // 查询该用户是否给指定评论点过赞
        AnswerCommentsLinkBean answerCommentsLink = answerCommentsHDServers.queryAnswerCommentsLink(answerCommentsLinkBean);
        // 查询指定评论
        AnswerCommentsHDVO answerComments = answerCommentsHDServers.queryAssginComments(answerCommentsLinkBean.getRid());
        if (answerCommentsLink != null) {   // 有点赞记录
            return "您已经点过赞啦！！！";
        } else {    // 无点赞记录
            // 新增一条点赞记录
            answerCommentsHDServers.addAnswerCommentsLinkfo(answerCommentsLinkBean);
            // 将指定评论点赞量 +1
            answerComments.setRead_count(answerComments.getRead_count() + 1);
            // 保存点赞量
            answerCommentsHDServers.updateAssignAnswerCommentsLink(answerComments);
            return "点赞成功！！！";
        }
    }
}
